#include <map>
#include <vector>
#include <array>
#include <any>
#include <variant>
#include <iostream>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sstream>
#include <fstream>
#include <set>
#include <unistd.h>


using namespace std;
#define COLUMN_MAX_SIZE     (32)
#define OP_MAX_SIZE         (16)
#define VALUE_MAX_SIZE      (32)
#define MAX_QUERY_SQL_LEN   (256)

typedef struct Condition {
    char column[COLUMN_MAX_SIZE];
    char op[OP_MAX_SIZE];
    char value[VALUE_MAX_SIZE];
} Condition;


/*数据文件路径*/
#define PART_FILE_PATH "/bin/part.txt"

/*sql语句文件路径*/
#define TEST_SQL_FILE_PATH "/bin/part_test_sql.txt"

/*清空字符数组*/
#define CLEAR_CHAR_ARRAY(a) memset(a, '\0', sizeof(a))

/*字符串数组转string*/
#define CHAR_ARRAY_TO_STRING(a) NULL == a ? string(""):string(a)

#define INIT_CONDITION(a) memset(&a, '\0', sizeof(Condition))

/*地址转PartItem指针*/
#define ADDRESS_TO_PARTITEM_PTR(a) (PartItem*)(a)

/*定义长度*/
#define MAX_ITEM_NUM        (8000000)       // 最大数据量
#define MAX_BUF_SIZE        (2000000)       // 查询结果最大数据量
#define BUF_SIZE_16         (16)
#define BUF_SIZE_32         (32)
#define BUF_SIZE_64         (64)
#define BUF_SIZE_128        (128)
#define BUF_SIZE_256        (256)
#define EMPTY_BUF_SIZE      (0)

/*自定义数据类型*/
typedef char char_16[BUF_SIZE_16 + 1];
typedef char char_32[BUF_SIZE_32 + 1];
typedef char char_64[BUF_SIZE_64 + 1];
typedef char char_128[BUF_SIZE_128 + 1];
typedef char char_256[BUF_SIZE_256 + 1];

/*测试用例数据量*/
#define WORKLOAD_SIZE_100M  (800000)
#define WORKLOAD_SIZE_150M  (1200000)
#define WORKLOAD_SIZE_200M  (1600000)
#define WORKLOAD_SIZE_250M  (2000000)
#define WORKLOAD_SIZE_500M  (4000000)
#define WORKLOAD_SIZE_1000M (8000000)

/*数据量与数据条目映射关系*/
map<string, size_t> work_load_item_num = {
    //{"100M", WORKLOAD_SIZE_100M},
    {"150M", WORKLOAD_SIZE_150M},
    {"200M", WORKLOAD_SIZE_200M},
    {"250M", WORKLOAD_SIZE_250M},
    {"500M", WORKLOAD_SIZE_500M},
    {"1000M", WORKLOAD_SIZE_1000M}
};

/*PART表字段名称*/
#define P_PARTKEY_FIELD         "P_PARTKEY"
#define P_NAME_FIELD            "P_NAME"
#define P_MFGR_FIELD            "P_MFGR"
#define P_BRAND_FIELD           "P_BRAND"
#define P_TYPE_FIELD            "P_TYPE"
#define P_SIZE_FIELD            "P_SIZE"
#define P_CONTAINER_FIELD       "P_CONTAINER"
#define P_RETAILPRICE_FIELD     "P_RETAILPRICE"
#define P_COMMENT_FIELD         "P_COMMENT"

/*按照字段名称为计数器构建下标索引*/
enum PartCounterIndex {
    P_PARTKEY_COUNTER = 0,
    P_NAME_COUNTER,
    P_MFGR_COUNTER,
    P_BRAND_COUNTER,
    P_TYPE_COUNTER,
    P_SIZE_COUNTER,
    P_CONTAINER_COUNTER,
    P_RETAILPRICE_COUNTER,
    P_COMMENT_COUNTER
};

/*字段名称集合*/
array<string, P_COMMENT_COUNTER + 1> PartFieldNameList = {
    "P_PARTKEY",
    "P_NAME",
    "P_MFGR",
    "P_BRAND",
    "P_TYPE",
    "P_SIZE",
    "P_CONTAINER",
    "P_RETAILPRICE",
    "P_COMMENT",
};

/*字段名称与字段索引映射*/
map<string, int32_t> part_field_index_map = {
    {"P_PARTKEY", P_PARTKEY_COUNTER},
    {"P_NAME", P_NAME_COUNTER},
    {"P_MFGR", P_MFGR_COUNTER},
    {"P_BRAND", P_BRAND_COUNTER},
    {"P_TYPE", P_TYPE_COUNTER},
    {"P_SIZE", P_SIZE_COUNTER},
    {"P_CONTAINER", P_CONTAINER_COUNTER},
    {"P_RETAILPRICE", P_RETAILPRICE_COUNTER},
    {"P_COMMENT", P_COMMENT_COUNTER},
};

/*数据条目*/
typedef struct PartItem {
    int32_t PartKey;
    char_64 Name;
    char_32 MFGR;
    char_16 Brand;
    char_32 Type;
    int32_t Size;
    char_16 Container;
    char_32 RetailPrice;
    char_32 Comment;
    PartItem() {
        PartKey = 0;
        Size = 0;
        CLEAR_CHAR_ARRAY(Name);
        CLEAR_CHAR_ARRAY(MFGR);
        CLEAR_CHAR_ARRAY(Brand);
        CLEAR_CHAR_ARRAY(Type);
        CLEAR_CHAR_ARRAY(Container);
        CLEAR_CHAR_ARRAY(RetailPrice);
        CLEAR_CHAR_ARRAY(Comment);
    }
} PartItem;
/*访问频次计数器*/
typedef map<int32_t, int16_t> Counter;
typedef array<Counter, P_COMMENT_COUNTER + 1> AccessCounter;

/*自定义Variant类型，包括int(0), string(1), double(2)三种变量类型*/
typedef variant<int32_t, string, double> Variant;

/*缓存桶, 用来存储<字段名称, 主键>*/
typedef map<Variant, vector<int32_t> > BucketItem;
typedef array<BucketItem, P_COMMENT_COUNTER + 1> PartBucket;

/*查询条件涉及的字段列表*/
typedef struct DAQueryContidion {
    size_t n_condition;             // 查询条件个数
    uint64_t condition_item;        // 查询条件
    DAQueryContidion(size_t _n_condition, uint64_t _condition_item) {
        n_condition = _n_condition;
        condition_item = _condition_item;
    }
} DAQueryContidion;

/*查询结果返回列表*/
typedef struct DAResultList {
    size_t n_result;                        // 结果数量
    uint64_t part_result_list;          // 结果首地址
    DAResultList(size_t _n_result, uint64_t _part_result_list) {
        n_result = _n_result;
        part_result_list = _part_result_list;
    }
} DAResultList;

/*构建桶之前的简单缓存实现(map使用自定义数据类型作为主键需要实现模板函数)*/
typedef map<std::string, std::set<int32_t>> DAQueryCache;
typedef struct DAQuerySQLCounter{
    std::string da_query_sql;
    int32_t frequency;
} DAQuerySQLCounter;



clock_t start1, stop1;                                // 时间参数
clock_t start2, stop2;
unsigned long long begin1, end1, total1 = 0;        // CPU时钟周期数
unsigned long long begin2, end2, total2 = 0;        // CPU时钟周期数
uint8_t buf[BUF_SIZE_128] = {0};                      // 无符号缓冲区
int retval = 0;                                     // ecall函数返回值


vector<string> querys;                           // sql语句

// 数据加密后存储在非安全区中，首地址传入TEE中，无需ocall直接访问
PartItem *part_items = new PartItem[MAX_ITEM_NUM + 1];
size_t part_items_size = 0;
// 相关数据结构定义
PartBucket part_bucket;     // 缓存桶
AccessCounter ac_counter;           // 访问频次计数器      

// 测量CPU时钟周期数
static __inline__ unsigned long long rdtsc(void)	 
{	 
	unsigned hi, lo; 	
	__asm__ __volatile__ ("rdtsc" : "=a"(lo),"=d"(hi));	 
	return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );	 
} 

// char* 转string
string toString(char* arr)
{   
    string str = string(arr);
    return str;
}

/**
 * @brief 打印查询结果
 * @param res_item 结果列表
 * @param res_len 结果长度
*/
void print_res(PartItem *res_item, size_t res_len)
{
    if (0 == res_len) {
        cout << "empty result list!" << endl;
        return;
    }
    for (size_t i = 0;i < res_len;i++) {
        cout << "=========================================\n";
        cout << i + 1 << ":" << endl;
        cout << "P_PARTKEY: " << res_item[i].PartKey << endl; 
        cout << "P_NAME: "<< res_item[i].Name << endl;
        cout << "P_MFGR: " << res_item[i].MFGR << endl;
        cout << "P_BRAND: " << res_item[i].Brand << endl;
        cout << "P_TYPE: " << res_item[i].Type << endl;
        cout << "P_SIZE: " << res_item[i].Size << endl;
        cout << "P_CONTAINER: " << res_item[i].Container << endl;
        cout << "P_RETAILPRICE: " << res_item[i].RetailPrice << endl;
        cout << "P_COMMENT: " << res_item[i].Comment << endl;
    }
}

/**
 * @brief 从文件中读取数据
 * @param workload_item_num_max 数据量
*/
void read_data(size_t workload_item_num_max)
{
    size_t i = 0;
    //以读入方式打开文件
    ifstream part_txt_data(PART_FILE_PATH, ios::in);
    if (!part_txt_data.is_open()) {
        cout << "Error: opening file failed" << endl;
        exit(-1);
    }
    else {
        string line;
        //声明一个字符串向量
        vector<string> words; 
        string word;
        // ------------读取数据-----------------
        // 读取标题行
        // getline(txt_data, line);
    
        istringstream sin;
        // 按行读取数据
        while (getline(part_txt_data, line)) {
            // 清空vector及字符串流,只存当前行的数据
            words.clear();
            sin.clear();
            sin.str(line);
            // cout << line << endl;
            //将字符串流sin中的字符读到字符串数组words中，以\t为分隔符
            while (getline(sin, word, '|')) {
                //cout << word << '\t';
                words.push_back(word); //将每一格中的数据逐个push
            }
            // P_PARTKEY
            part_items[i].PartKey = atoi(words[P_PARTKEY_COUNTER].c_str());
            // P_NAME
            memcpy(part_items[i].Name, words[P_NAME_COUNTER].c_str(), words[P_NAME_COUNTER].length());
            // P_MFGR
            memcpy(part_items[i].MFGR, words[P_MFGR_COUNTER].c_str(), words[P_MFGR_COUNTER].length());
            // P_BRAND
            memcpy(part_items[i].Brand, words[P_BRAND_COUNTER].c_str(), words[P_BRAND_COUNTER].length());
            // P_TYPE
            memcpy(part_items[i].Type, words[P_TYPE_COUNTER].c_str(), words[P_TYPE_COUNTER].length());
            // P_SIZE
            part_items[i].Size = atoi(words[P_SIZE_COUNTER].c_str());
            // P_CONTAINER
            memcpy(part_items[i].Container, words[P_CONTAINER_COUNTER].c_str(), words[P_CONTAINER_COUNTER].length());
            // P_RETAILPRICE
            memcpy(part_items[i].RetailPrice, words[P_RETAILPRICE_COUNTER].c_str(), words[P_RETAILPRICE_COUNTER].length());
            // P_COMMENT
            memcpy(part_items[i].Comment, words[P_COMMENT_COUNTER].c_str(), words[P_COMMENT_COUNTER].length());
            // 下标自增
            i++;
            
            if (i == workload_item_num_max) {
                break;
            }
        }
        part_txt_data.close();
    }
    // 数据条目数量
    part_items_size = i;

}

void read_sql() 
{
     // 打开文件
    ifstream sql_data(TEST_SQL_FILE_PATH, ios::in);

    if (!sql_data.is_open()) {
        cout << "Error: opening file fail" << endl;
        exit(1);
    }
    string line;
    int i = 0;
    while (getline(sql_data, line)) {
    	if (i >= 1000) {
    	    querys.push_back(line);
    	}
    	i++;
        
    }
    // 关闭文件
    
    cout << "query nums: " << querys.size() << endl;
    sql_data.close();
}

// 删除字符串开头的空格
void remove_space_in_string(string &conditionsStr)
{
    string::iterator it = conditionsStr.begin();     //指向string类的迭代器。你可以理解为指针
	while (*it == ' ') {
        conditionsStr.erase(it);
        it = conditionsStr.begin();
    }
}


/**
* @brief 解析 SQL 查询语句中的条件
* @param query SQL查询语句
*/ 
vector<Condition> parse_conditions(const string &query) {
    vector<Condition> conditions;

    // 查询语句中条件部分以 WHERE 关键字开头
    size_t wherePos = query.find("WHERE");
    if (wherePos != string::npos) {
        // 获取 WHERE 关键字之后的条件部分
        string conditionsStr = query.substr(wherePos + 5);

        // 以 AND 连接多个条件，将其分割为多个子条件
        size_t andPos = 0;

        // 解析剩余的最后一个条件
        remove_space_in_string(conditionsStr);
        size_t spacePos = conditionsStr.find(" ");
        string column = conditionsStr.substr(0, spacePos);
        conditionsStr = conditionsStr.substr(spacePos + 1);

        size_t opPos = conditionsStr.find_first_of("<>=");
        string op = conditionsStr.substr(0, opPos + 1);
        conditionsStr = conditionsStr.substr(opPos + 1);

        // 移除值周围的空格
        conditionsStr.erase(0, conditionsStr.find_first_not_of(" "));

        // 解析值
        string value;
        if (conditionsStr.front() == '\'' || conditionsStr.front() == '"') {
            // 处理字符串值
            size_t endQuotePos = conditionsStr.find(conditionsStr.front(), 1);
            value = conditionsStr.substr(1, endQuotePos - 1);
        } else {
            // 处理非字符串值
            size_t spaceAfterValuePos = conditionsStr.find(" ");
            value = conditionsStr.substr(0, spaceAfterValuePos);
        }

        // 添加解析得到的条件到条件向量中
        Condition condition;
        INIT_CONDITION(condition);
        // memset(&condition, '\0', sizeof(Condition));
        memcpy(condition.column, column.c_str(), column.length());
        memcpy(condition.op, op.c_str(), op.length());
        memcpy(condition.value, value.c_str(), value.length());
        conditions.push_back(condition);
    }

    return conditions;
}

/**
* @brief 根据计数器调整桶
* @param counter_threshhold 计数器阈值，当计数器值高于阈值时，数据加入bucket
*/
int32_t AdjustBucket()
{
    // 开始往桶里面丢，桶的个数与字段个数相同，桶里面应该记录<字段值，[主码索引]>
    for (size_t i = 0;i <= P_COMMENT_COUNTER;i++) {
        if (0 == ac_counter[i].size()) {    // 如果当前字段计数器中没有内容，继续遍历下一个计数器
            continue;
        }
        // 如果该字段计数器中有内容，就开始构建桶
        for (Counter::iterator it = ac_counter[i].begin(); it != ac_counter[i].end(); it++) {
            if (it->second > 0) {
                int index = it->first;
                switch (i) {
                    case P_PARTKEY_COUNTER: {
                        part_bucket[i][part_items[index].PartKey].push_back(it->first);
                        break;
                    }
                    case P_NAME_COUNTER: {
                        part_bucket[i][part_items[index].Name].push_back(it->first);
                        break;
                    }
                    case P_MFGR_COUNTER: {
                        part_bucket[i][part_items[index].MFGR].push_back(it->first);
                        break;
                    }
                    case P_BRAND_COUNTER: {
                        part_bucket[i][part_items[index].Brand].push_back(it->first);
                        break;
                    }
                    case P_TYPE_COUNTER: {
                        part_bucket[i][part_items[index].Type].push_back(it->first);
                        break;
                    }
                    case P_SIZE_COUNTER: {
                        part_bucket[i][part_items[index].Size].push_back(it->first);
                        break;
                    }
                    case P_CONTAINER_COUNTER: {
                        part_bucket[i][part_items[index].Container].push_back(it->first);
                        break;
                    }
                    case P_RETAILPRICE_COUNTER: {
                        part_bucket[i][part_items[index].RetailPrice].push_back(it->first);
                        break;
                    }
                    case P_COMMENT_COUNTER: {
                        part_bucket[i][part_items[index].Comment].push_back(it->first);
                        break;
                    }
                    default: break;
                }
                // 构建后计数器内容清0
                it->second = 0;
            }
        }
    }
    return 0;
}

/**
 * @brief 更新计数器内容
 * @param part_field_index 字段索引
 * @param index 数据索引
*/
void update_access_counter(int32_t part_field_index, int32_t index)
{
    if (ac_counter[part_field_index].find(index) != ac_counter[part_field_index].end()) {
        ac_counter[part_field_index][index]++;   // 更新计数器
    } else {
        ac_counter[part_field_index][index] = 0; // 首次访问初始化计数器
    }
}

/**
* @brief 从customer数据表中搜索数据
* @param field_index 查找字段索引
* @param part_item_tmp 暂存查找条件数据
* @param da_result_list 存储查询结果
*/
int32_t SearchFromTable(int32_t part_field_index, PartItem* part_item_tmp, DAResultList *da_result_list)
{
    Variant var_tar, var_src;
    int32_t type_id = -1;
    PartItem *result_list = ADDRESS_TO_PARTITEM_PTR(da_result_list->part_result_list);
    
    // 构建桶之前，检查查询是否在缓存中
    
    // if (0 == SearchFromQueryCache(da_result_list, part_field_index)) {    
    //     goto out;   // 在缓存中查找到数据后一样需要更新计数器
    // }

    switch (part_field_index) {
        case P_PARTKEY_COUNTER: {
            var_tar = part_item_tmp->PartKey;
            break;
        }
        case P_NAME_COUNTER: {
            var_tar = CHAR_ARRAY_TO_STRING(part_item_tmp->Name);
            break;
        }
        case P_MFGR_COUNTER: {
            var_tar = CHAR_ARRAY_TO_STRING(part_item_tmp->MFGR);
            break;
        }
        case P_BRAND_COUNTER: {
            var_tar = CHAR_ARRAY_TO_STRING(part_item_tmp->Brand);
            break;
        }
        case P_TYPE_COUNTER: {
            var_tar = CHAR_ARRAY_TO_STRING(part_item_tmp->Type);
            break;
        }
        case P_SIZE_COUNTER: {
            var_tar = part_item_tmp->Size;
            break;
        }
        case P_CONTAINER_COUNTER: {
            var_tar = CHAR_ARRAY_TO_STRING(part_item_tmp->Container);
            break;
        }
        case P_RETAILPRICE_COUNTER: {
            var_tar = CHAR_ARRAY_TO_STRING(part_item_tmp->RetailPrice);
            break;
        }
        case P_COMMENT_COUNTER: {
            var_tar = CHAR_ARRAY_TO_STRING(part_item_tmp->Comment);
            break;
        }

    }
    type_id = var_tar.index();
    // print(type_id);
    // 从数据表中查找
    for (size_t i = 0;i < part_items_size;i++) {
        switch (part_field_index) {
            case P_PARTKEY_COUNTER: {
                var_src = part_items[i].PartKey;
                break;
            }
            case P_NAME_COUNTER: {
                var_src = CHAR_ARRAY_TO_STRING(part_items[i].Name);
                break;
            }
            case P_MFGR_COUNTER: {
                var_src = CHAR_ARRAY_TO_STRING(part_items[i].MFGR);
                break;
            }
            case P_BRAND_COUNTER: {
                var_src = CHAR_ARRAY_TO_STRING(part_items[i].Brand);
                break;
            }
            case P_TYPE_COUNTER: {
                var_src = CHAR_ARRAY_TO_STRING(part_items[i].Type);
                break;
            }
            case P_SIZE_COUNTER: {
                var_src = part_items[i].Size;
                break;
            }
            case P_CONTAINER_COUNTER: {
                var_src = CHAR_ARRAY_TO_STRING(part_items[i].Container);
                break;
            }
            case P_RETAILPRICE_COUNTER: {
                var_src = CHAR_ARRAY_TO_STRING(part_items[i].RetailPrice);
                break;
            }
            case P_COMMENT_COUNTER: {
                var_src = CHAR_ARRAY_TO_STRING(part_items[i].Comment);
                break;
            }
        }

        if (var_src == var_tar) {
            memcpy(&result_list[da_result_list->n_result++], &part_items[i], sizeof(PartItem)); // 将结果从表中拷贝到结果中
            // 将查询结果加入缓存
            update_access_counter(part_field_index, i);
        }
    }
   
    return 0;
}


/**
* @brief 从bucket中查询数据
* @param part_field_index 查找字段索引
* @param part_item_tmp 暂存查询条件的数据项
* @param da_result_list 存储查询结果
*/
int32_t SearchFromBucket(int32_t part_field_index, PartItem* part_item_tmp, DAResultList *da_result_list)
{   
    Variant var_tar, var_src;               // 比较变量
    int32_t type_id = -1;                   // 数据类型
    PartItem *result_list = ADDRESS_TO_PARTITEM_PTR(da_result_list->part_result_list);
    switch(part_field_index) {
        case P_PARTKEY_COUNTER: {
            var_tar = part_item_tmp->PartKey;
            break;
        }
        case P_NAME_COUNTER: {
            var_tar = CHAR_ARRAY_TO_STRING(part_item_tmp->Name);
            break;
        }
        case P_MFGR_COUNTER: {
            var_tar = CHAR_ARRAY_TO_STRING(part_item_tmp->MFGR);
            break;
        }
        case P_BRAND_COUNTER: {
            var_tar = CHAR_ARRAY_TO_STRING(part_item_tmp->Brand);
            break;
        }
        case P_TYPE_COUNTER: {
            var_tar = CHAR_ARRAY_TO_STRING(part_item_tmp->Type);
            break;
        }
        case P_SIZE_COUNTER: {
            var_tar = part_item_tmp->Size;
            break;
        }
        case P_CONTAINER_COUNTER: {
            var_tar = CHAR_ARRAY_TO_STRING(part_item_tmp->Container);
            break;
        }
        case P_RETAILPRICE_COUNTER: {
            var_tar = CHAR_ARRAY_TO_STRING(part_item_tmp->RetailPrice);
            break;
        }
        case P_COMMENT_COUNTER: {
            var_tar = CHAR_ARRAY_TO_STRING(part_item_tmp->Comment);
            break;
        }
        default: break;
    }

    type_id = var_tar.index();  // 获取变量数据类型
    // 先在桶里找
    for (BucketItem::iterator it = part_bucket[part_field_index].begin();it != part_bucket[part_field_index].end(); it++) {
        var_src = it->first;
        if (var_tar == var_src) { // 如果相等，说明在bucket中找到了数据
            // print_int(it->second.size());
            da_result_list->n_result = it->second.size();
            for (size_t i = 0;i < it->second.size();i++) {
                memcpy(&result_list[i], &part_items[it->second[i]], sizeof(PartItem));
            }
        }
    }
    
    // 找不到再去表里搜
    if (0 == da_result_list->n_result) {
        SearchFromTable(part_field_index, part_item_tmp, da_result_list);
    }

    return 0;
}


/**
* @brief 查询customer表中的数据
* @param da_query_sql_str sql语句字符串(c风格)
* @param da_result_list_addr     查询结果地址
*/
int32_t get(string da_query_sql, DAResultList *da_result_list)
{
    int32_t field_index = -1;   // 查询字段索引
    vector<Condition> conditions = parse_conditions(da_query_sql);
    // 获取查询条件

    string op_str = CHAR_ARRAY_TO_STRING(conditions[0].op);
    string val_str = CHAR_ARRAY_TO_STRING(conditions[0].value);
    string column_str = CHAR_ARRAY_TO_STRING(conditions[0].column);
    
    // 暂存查询条件到数据
    PartItem *part_item_tmp = new PartItem;

    
    // 获取查询字段索引(没有获取到正确的field_index)
    if (part_field_index_map.find(column_str) != part_field_index_map.end()) {
        field_index = part_field_index_map[column_str];
        // print_int(field_index);
    }

    // 根据字段索引查询
    switch (field_index) {
        case P_PARTKEY_COUNTER: {
            part_item_tmp->PartKey = stoi(val_str);
            SearchFromBucket(P_PARTKEY_COUNTER, part_item_tmp, da_result_list);
            break;
        }
        case P_NAME_COUNTER: {
            memcpy(part_item_tmp->Name, val_str.c_str(), val_str.length());
            SearchFromBucket(P_NAME_COUNTER, part_item_tmp, da_result_list);
            break;
        }
        case P_MFGR_COUNTER: {
            memcpy(part_item_tmp->MFGR, val_str.c_str(), val_str.length());
            // print_str(part_item_tmp->MFGR);
            SearchFromBucket(P_MFGR_COUNTER, part_item_tmp, da_result_list);
            break;
        }
        case P_BRAND_COUNTER: {
            memcpy(part_item_tmp->Brand, val_str.c_str(), val_str.length());
            SearchFromBucket(P_BRAND_COUNTER, part_item_tmp, da_result_list);
            break;
        }
        case P_TYPE_COUNTER: {
            memcpy(part_item_tmp->Type, val_str.c_str(), val_str.length());
            SearchFromBucket(P_TYPE_COUNTER, part_item_tmp, da_result_list);
            break;
        }
        case P_SIZE_COUNTER: {
            part_item_tmp->Size = stoi(val_str.c_str());
            SearchFromBucket(P_SIZE_COUNTER, part_item_tmp, da_result_list);
            break;
        }
        case P_CONTAINER_COUNTER: {
            memcpy(part_item_tmp->Container, val_str.c_str(), val_str.length());
            SearchFromBucket(P_CONTAINER_COUNTER, part_item_tmp, da_result_list);
            break;
        }
        case P_RETAILPRICE_COUNTER: {
            memcpy(part_item_tmp->RetailPrice, val_str.c_str(), val_str.length());
            SearchFromBucket(P_RETAILPRICE_COUNTER, part_item_tmp, da_result_list);
            break;
        }
        case P_COMMENT_COUNTER: {
            memcpy(part_item_tmp->Comment, val_str.c_str(), val_str.length());
            SearchFromBucket(P_COMMENT_COUNTER, part_item_tmp, da_result_list);
            break;
        }
        default: break;
    }
    return 0;
}


int main(int argc, char* argv[])
{
    // 读取测试sql语句
    // read_sql();
    string test_sql = "SELECT * FROM PART WHERE P_MFGR = 'helloworld';";
    vector<uint64_t> table_time_cost;
    cout << "read test sql success!" << endl;
    // 暂存CPU始终周期数和时间消耗


    size_t res_len = 0;
    // 开始执行
    PartItem* res_item = new PartItem[MAX_BUF_SIZE + 1];
    for (auto workload: work_load_item_num) {
        vector<clock_t> time_cost;
        vector<uint64_t> cycle_cost;
        // 生成数据
       	//read_data(workload.second);  
	part_items_size = workload.second;  
        cout << workload.first << endl;
        for (int i = 0;i < 1000;i++) {
            DAResultList *da_result_list = new DAResultList(EMPTY_BUF_SIZE, (uint64_t)res_item);
            // 返回结果集合

            start1 = clock();
            begin1 = rdtsc();
            // 查询数据
            //get(querys[i], da_result_list);
            get(test_sql, da_result_list);
	    //cout << i << ": " << da_result_list->n_result << endl;
            // 查询数据
            end1 = rdtsc();
            stop1 = clock();
            total1 = end1 - begin1;
            res_len = 0;
        
            time_cost.push_back(stop1 - start1);
            cycle_cost.push_back(total1);
        }
        uint64_t sum1 = 0, sum2 = 0;
        for (int i = 0;i < time_cost.size();i++) {
	        sum1 += time_cost[i];
        }
        table_time_cost.push_back(sum1/1000);
    }
    // 输出最终结果
    int index = 0;
    for (auto it: work_load_item_num) {
        cout << it.first << ": " << table_time_cost[index++] << endl;
    }
    return 0;
}


